﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data;

using SQLWrapper;
using BartonExtension;

using Model.App;

using Services.Common;

namespace Services.Share
{
    public class ISMShareServiceProvider : ISMServiceProvider
    {
        public const string SHARE_CONTENT = "share_content";
        public override Task<ISMServiceResult> doProcess(ISMServiceModel model)
        {
            if(model.operationIdentifier == SHARE_CONTENT)
            {
                ISMShareContentRequest request = model.GetState<ISMShareContentRequest>();
                return ShareContent(request);
            }
            else
            {
                return base.defaultProcess();
            }
        }
        private Task<ISMServiceResult> ShareContent(ISMShareContentRequest request)
        {
            return Task.Factory.StartNew<ISMServiceResult>(() =>
            {
                ISMServiceResult result = new ISMServiceResult();
                MySQLOperationManager.doSQLOperation(cmd =>
                {
                    cmd.CommandText = "proc_client_share_content";
                    cmd.Parameters.Add("?_uid", request.uid, ParameterDirection.Input);
                    cmd.Parameters.Add("?_share_link", request.link, ParameterDirection.Input);
                    cmd.Parameters.Add("?_share_title", request.title, ParameterDirection.Input);
                    cmd.Parameters.Add("?_share_desc", request.desc, ParameterDirection.Input);
                    cmd.Parameters.Add("?_share_image", request.image_url == null ? "" : request.image_url, ParameterDirection.Input);
                    cmd.Parameters.Add("?_token", request.token, ParameterDirection.Input);
                    cmd.Parameters.Add("?share_id", MySql.Data.MySqlClient.MySqlDbType.UInt64, ParameterDirection.Output);
                    cmd.Parameters.Add("?errorcode", MySql.Data.MySqlClient.MySqlDbType.Int32, ParameterDirection.Output);
                    cmd.ExecuteNonQuery();
                    int errorcode = cmd.Parameters["?errorcode"].Value.toInt32().Value;
                    if(errorcode == -1)
                    {
                        result.isOperationSuccess = false;
                        result.errorCode = ISMServiceResultErrorCodes.ERR_CODE_ISM_TOKEN_ERROR;
                        result.errorMsg = "Token 验证失败";
                    }
                    else if(errorcode == 0)
                    {
                        UInt64 share_id = cmd.Parameters["?share_id"].Value.toUInt64().Value;
                        result.dbReturningState = share_id;
                    }
                },
                e =>
                {
                    if(e != null)
                    {
                        result.isOperationSuccess = false;
                        result.errorMsg = e;
                        result.errorCode = ISMServiceResultErrorCodes.ERR_CODE_ISM_EXCEPTION_ERR;
                    }
                },System.Data.CommandType.StoredProcedure,false);
                return result;
            });
        }
    }
}
